/**
  ******************************************************************************
  * @file 	
  * @brief  header file 
  ******************************************************************************
  */

#ifndef __ZBI2C_H
#define __ZBI2C_H

#include "main.h"

#define USEI2C1

#define true							1
#define false							0

typedef enum{ DMA_TX = 0, DMA_RX = 1 }DMADirection_TypeDef;

/* -------
i2c1
pb6=scl
pb7=sda
pb8=scl alt
pb9=sda alt
---------- */

// ------------ i2c1
#ifdef USEI2C1
#define P8P9_ALTFXN					0							// are pins 8 and 9 used for i2c1 ? 
#define I2C_MAIN                    I2C1
#define I2C_CLK                     RCC_APB1Periph_I2C1
#define I2C_SCL_PIN                 GPIO_Pin_6                  /* we use 8 & 9 */
#define I2C_SCL_GPIO_PORT           GPIOB                       /* GPIOB */
#define I2C_SCL_GPIO_CLK            RCC_APB2Periph_GPIOB
#define I2C_SDA_PIN                 GPIO_Pin_7                  /* we use 8 & 9 */
#define I2C_SDA_GPIO_PORT           GPIOB                       /* GPIOB */
#define I2C_SDA_GPIO_CLK            RCC_APB2Periph_GPIOB
//#define I2C_SMBUSALERT_PIN          GPIO_Pin_5                  /* PB.05 */
//#define I2C_SMBUSALERT_GPIO_PORT    GPIOB                       /* GPIOB */
//#define I2C_SMBUSALERT_GPIO_CLK     RCC_APB2Periph_GPIOB        /* we dont use this */
#define I2C_DR                      ((uint32_t)0x40005410)      /* pg.42(stm) 0x4000 5400 - 0x4000 57FF I2C1, + 0x10 is DR */
#endif

// ------------ i2c2
#ifdef USEI2C2
#define I2C_MAIN                    I2C2
#define I2C_CLK                     RCC_APB1Periph_I2C2
#define I2C_SCL_PIN                 GPIO_Pin_10                  /* we use 8 & 9 */
#define I2C_SCL_GPIO_PORT           GPIOB                       /* GPIOB */
#define I2C_SCL_GPIO_CLK            RCC_APB2Periph_GPIOB
#define I2C_SDA_PIN                 GPIO_Pin_11                  /* we use 8 & 9 */
#define I2C_SDA_GPIO_PORT           GPIOB                       /* GPIOB */
#define I2C_SDA_GPIO_CLK            RCC_APB2Periph_GPIOB
//#define I2C_SMBUSALERT_PIN          GPIO_Pin_5                  /* PB.05 */
//#define I2C_SMBUSALERT_GPIO_PORT    GPIOB                       /* GPIOB */
//#define I2C_SMBUSALERT_GPIO_CLK     RCC_APB2Periph_GPIOB        /* we dont use this */
#define I2C_DR                      ((uint32_t)0x40005810)      /* pg.42(stm) 0x4000 5400 - 0x4000 57FF I2C1, + 0x10 is DR */

#endif

#define LONG_TIMEOUT   				((uint32_t)0x00FFF)
#define FLAG_TIMEOUT   				((uint32_t)0x00FFF)

#define DMA_I2C_CLK                     RCC_AHBPeriph_DMA1
#define DMA_I2C_TX_CHANNEL              DMA1_Channel6
#define DMA_I2C_RX_CHANNEL              DMA1_Channel7
#define DMA_I2C_TX_TCFLAG               DMA1_FLAG_TC6
#define DMA_I2C_RX_TCFLAG               DMA1_FLAG_TC7

void initI2CMain(void);

void disableI2CMain(void);

void updateI2CMain(void);

void initI2CMainGpio(void);


static void DMA_Config(DMADirection_TypeDef Direction, uint8_t* buffer, uint8_t NumData);

u16 ReadReg(u8 devAddr, u8 intAddr);

u16 WriteReg(uint8_t devAddr, u8 intAddr, uint16_t value);

u16 TIMEOUT_UserCallback(unsigned char state);

#endif

